<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="avatar_box">
        <img class="imglogo" src="../assets/logo.png" alt="">
      </div>
      <!--表单区域
      1. :rules是饿了么的语法 ，不是等于v-bind
      2. ref="loginFormRef"相当于起别名，拿到这个就可以拿到整个的实例对象
      -->
      <el-tabs stretch  v-model="activeTab">
        <el-tab-pane label="用户登录" name="first" >

          <el-form ref="loginFormRef" v-bind:model="loginForm" :rules="loginFormRules"
                   label-width="80px" class="login_form">
            <el-form-item prop="username" label="用户名">
              <el-input v-model="loginForm.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password"></el-input>
            </el-form-item>
            <!--按钮区域-->
            <el-form-item class="btns">
              <!--<router-link to="/test">-->
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="success" @click="register">注册</el-button>
                <el-button type="info" @click="resetloginForm">重置</el-button>
                <!--&lt;!&ndash;登录&ndash;&gt;-->
              <!--</router-link>-->
                <!--<el-button type="primary" >-->
                  <!--&lt;!&ndash;<router-link to="/test">登录&ndash;&gt;-->
                  <!--<router-link to="/test">登录-->
                  <!--</router-link>-->
                <!--</el-button>-->
                <!--登录-->
              <!--<el-button type="primary" @click="login">登录</el-button>-->


            </el-form-item>
          </el-form>

        </el-tab-pane>
        <el-tab-pane label="微信登录" name="second">
          <!--<img class="imgwx" src="../assets/wxlogin.png" alt="">-->

          <el-form ref="loginFormRef" v-bind:model="loginForm" :rules="loginFormRules"
                   label-width="80px" class="login_form">
            <el-form-item prop="username" label="微信号码">
              <el-input v-model="loginForm.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="微信密码">
              <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password"></el-input>
            </el-form-item>
            <!--按钮区域-->
            <el-form-item class="btns">
              <el-button type="primary" >登录</el-button>
              <el-button type="info" @click="resetloginForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        // 这是登录表单的数据绑定对象
        loginForm: {
            // username: "lyh",
            // password: "123456"
          "username": "lyh1",
          "password": "1234567"
        },
        loginFormRules:{
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            //触发条件：当不在框内，失去焦点的时候
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
          ]
        },
        activeTab: 'first'
      }
    },
    methods:{
      //重置整个表单数据
      resetloginForm(){
        // console.log(this);
        this.$refs.loginFormRef.resetFields();
      },
      login(){
          //表单预验证 $refs.loginFormRef
          this.$refs.loginFormRef.validate(async valid=>{
              //打印预验证的值
              console.log(valid)
              //不合适就返回
              if (!valid) return;
              //接收表单的值,并发送请求.{data:res} 代表从res中单独把data值拿出来，不要其他的。
              // const { data: res } = await this.$http.post("force",this.loginForm);
              const { data: res } = await this.$http.post("/user/login/force",
              {
                  "userName":this.loginForm.username,
                  "password":this.loginForm.password,
               });
              console.log(res);

              if (res.meta.status !== 200) return this.$message.error('登录失败！');
              //打印成功消息给用户
              this.$message({message:'登录成功！',type:'success',duration:3000});
              //最新的在authUserID里面，把authUserID储存在sessionStorage
              window.sessionStorage.setItem("token",res.data.authUserID);
              window.sessionStorage.setItem("userName",res.data.userName);

              //跳转到home页
              this.$router.push("/home");
              console.log("我要跳转了")
          });
         },
      register(){
          this.$router.push("/register");
      }
    }
  }
</script>

<!--scoped:样式只在本组件生效，不会影响到其他组件-->
<!--less:支持less语法-->
<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}
.login_box{
    background-color: #fff;
    width: 450px;
    height: 300px;
    border-radius: 30px;
    position: absolute;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
  }
.avatar_box{
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.imglogo {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.btns{
  display: flex;
  justify-content: flex-end;
}

.el-tabs {
position: absolute;
bottom: 0;
width: 100%;
padding: 0 10px;
box-sizing: border-box;

}

/*.login_form {*/
  /*position: absolute;*/
  /*bottom: 0;*/
  /*width: 100%;*/
  /*padding: 0 10px;*/
  /*box-sizing: border-box;*/
/*}*/


</style>


